package face.pay.web.oem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.enterprise.service.IprofitBaseService;
import face.pay.dao.MchAgencyInfoDao;
import face.pay.dao.ProfitInfoDao;
import face.pay.dao.UserInfoDao;
import face.pay.entity.ProfitInfo;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.oem.service.IUserManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Stream;

@Slf4j
@Service
public class UserManageServiceImpl implements IUserManageService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private ProfitInfoDao profitInfoDao;

    @Autowired
    private MchAgencyInfoDao mchAgencyInfoDao;


    @Override
    public PageData queryPage(UserInfo userInfo, PageParam pageParam) {

        PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());

        List<UserInfo> userInfoList = userInfoDao.queryUserWithPar(userInfo);

        PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(userInfoList);

        PageData<UserInfo> pageData = new PageData<UserInfo>(pageInfo.getTotal(), userInfoList);

        return pageData;
    }

    /**
     * 修改用户删除标记  0：未删除 1：已删除
     *
     * @param userInfo
     * @return
     */
    @Override
    public ResultMessage deleted(UserInfo userInfo) {
        String message = "更新失败";
        int i = userInfoDao.deleted(userInfo);
        if (i != 0) {
            log.info("用户注销ID为：" + userInfo.getId());
            return ResultMessage.createSuccess();
        }
        return ResultMessage.createFail(message);
    }

    /**
     * 修改用户状态 -1：锁定 0：过期 1：正常
     *
     * @param userInfo
     * @return
     */
    @Override
    public ResultMessage modifyUserState(UserInfo userInfo) {
        String message = "更新失败";
        int i = userInfoDao.updateUserState(userInfo);
        if (i != 0) {
            log.info("用户状态更改成功ID为：" + userInfo.getId());
            return ResultMessage.createSuccess();
        }
        return ResultMessage.createFail(message);
    }

    /**
     * 修改代理商推荐人
     *
     * @param userInfo
     * @return
     */
    @Override
    public ResultMessage modifyUserRecommend(UserInfo userInfo) {
        if ("AGENCY".equals(userInfo.getType())) {
            userInfo.setAgencyId(null);
            userInfoDao.updateRecommendId(userInfo);
            log.info("代理商id为" + userInfo.getId() + "的推荐人id修改成为：" + userInfo.getRecommendId());
            return ResultMessage.createSuccess();
        } else {
            //查询最低分润
            ProfitInfo profitInfo = profitInfoDao.queryMinProfit(userInfo.getRecommendId());
            userInfo.setProfitId(profitInfo.getId());
            int i = userInfoDao.updateRecommendId(userInfo);
            log.info("商家id为" + userInfo.getId() + "的推荐人id修改成为：" + userInfo.getRecommendId());
            if (i != 0) {
                //删除商家代理商关系表中的数据
                mchAgencyInfoDao.deleteMchUserChangeAgencyId(userInfo.getId());
            }
            return ResultMessage.createSuccess();
        }
    }

    /**
     * 修改用户基本信息
     *
     * @param userInfo
     * @return
     */
    @Override
    public ResultMessage modifyUserInfo(UserInfo userInfo) {
        try {
            userInfoDao.modifyUserInfo(userInfo);
            return ResultMessage.createSuccess();
        }catch (Exception e){
            return ResultMessage.createFail("用户名或手机号码有重复!");
        }
    }
}
